/**
 * Created by liubin on 2014/07/04.
 */
'use strict';

define(['angular', 'controllers', 'user/userService'], function (angular, controllers) {
    // User列表
    controllers.controller('userListCtrl', ['$scope', '$rootScope', 'userListService', function ($scope, $rootScope, userListService) {
        console.log('userListCtrl');
        $scope.users = [];
        $scope.baseUrl = urlRoute.getUrl('userlist') + '/';
        userListService.get({}, {}, function (success) {
                angular.forEach(success.data, function (value, key) {
                    console.log(value);
                    $scope.users.push(value);
                });
            }
        );

    }]).controller('userAddCtrl', ['$scope', '$window','$rootScope', 'userListService', 'alertService', 'type',
        function ($scope, $window, $rootScope, userListService, alertService, type) {
            console.log('userAddCtrl');
            console.log(type);
            alertService($scope);
            $scope.type = type;
            $scope.save = function (user) {
                console.log($scope.user);
                $scope.closeAllAlert();
                if ($scope.user.pwd == $scope.user.repeat_pwd) {
                    userListService.post({},
                        {
                            name: $scope.user.name,
                            real_name: $scope.user.real_name,
                            pwd: $scope.user.pwd
                        },
                        function (success) {
                            if (success.success) {
                                $window.location.href = urlRoute.getUrl('userlist');
                            }
                            else {
                                angular.forEach(success.messages, function (value, key) {
                                    $scope.addAlert('danger', value);
                                });
                            }
                        }
                    );
                }
                else {
                    $scope.addAlert('danger', _('Repeat password is not the same'));
                }
            }
        }]).controller('userEditCtrl', ['$scope', '$window', '$rootScope', '$routeParams', 'userService',
        'alertService', 'type',
        function ($scope, $window, $rootScope, $routeParams, userService, alertService, type) {
            console.log('userEditCtrl');
            console.log(type);
            console.log($routeParams.id);
            $scope.type = type;
            alertService($scope);
            userService.get(
                {id: $routeParams.id},
                {},
                function (success) {
                    console.log(success);
                    if (success.success) {
                        console.log(success.data);
                        $scope.user = success.data;
                    }
                    else {
                        //todo: add user get failure
                    }
                }
            );
            $scope.save = function (user) {
                console.log($scope.user);
                $scope.closeAllAlert();
                if ($scope.user.id) {
                    if ($scope.user.pwd == $scope.user.repeat_pwd) {
                        userService.put(
                            {id: $routeParams.id},
                            {
                                name: $scope.user.name,
                                real_name: $scope.user.real_name,
                                pwd: $scope.user.pwd
                            },
                            function (success) {
                                console.log(success);
                                if (success.success) {
                                    $window.location.href = urlRoute.getUrl('userlist');
                                }
                                else {
                                    angular.forEach(success.messages, function (value, key) {
                                        $scope.addAlert('danger', value);
                                    });
                                }
                            }
                        );
                    }
                    else {
                        $scope.addAlert('danger', _('Repeat password is not the same'));
                    }
                }
                else {
                    $scope.addAlert('danger', _('User loaded failure.Can not be saved'));
                }
            }

        }]).controller('userRemoveCtrl', ['$scope', '$modal', '$window', 'userService',
        function ($scope, $modal, $window, userService) {
            console.log('userRemoveCtrl');
            $scope.remove = function (size) {
                console.log('remove');
                console.log($scope.user);
                console.log($scope.user.id);
                var modalInstance = $modal.open({
                    templateUrl: '/confirm',
                    backdrop: 'static',
                    controller: 'confirmCtrl',
                    resolve: {
                        title: function () {
                            return '确认';
                        },
                        content: function () {
                            return '是否要删除指定用户?';
                        }
                    },
                    size: size
                });
                modalInstance.result.then(
                    function () {
                        console.log('saved');
                        userService.delete(
                            {id: $scope.user.id},
                            {},
                            function (success) {
                                console.log(success);
                                if (success.success) {
                                    $window.location.href = urlRoute.getUrl('userlist');
                                }
                                else {
                                    angular.forEach(success.messages, function (value, key) {
                                        $scope.addAlert('danger', value);
                                    });
                                }
                            }
                        );
                    },
                    function () {
                        console.log('cancel');
                    }
                );
            }
        }]);
});